import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {
  get_QuestionsType,
  subjectItem,
  examTypeItem,
  getQuestions_newItem,
} from '@/utils/interface';
import {
  getGetQuestionsType,
  getInsertQuestionsType,
  postDelQuestionsType,
  getSubject,
  getExamType,
  postQuestions,
  getQuestions_new,
  getQuestions_condition,
  getQuestions_update,
} from '@/services/index';
export interface QuestionModelState {
  questionsType: get_QuestionsType[]; //所有试题类型
  subject: subjectItem[]; //所有课程
  examType: examTypeItem[]; //所有考试类型
  getQuestions_newlist: getQuestions_newItem[]; //试题
}

export interface QuestionModelType {
  namespace: 'question';
  state: QuestionModelState;
  effects: {
    getGetQuestionsType: Effect; //获取所有的试题类型
    getInsertQuestionsType: Effect; //添加试题类型
    postDelQuestionsType: Effect; //删除指定的试题类型
    getSubject: Effect; //获取所有的课程
    getExamType: Effect; //获取所有的考试类型
    postQuestions: Effect; //添加试题接口
    getQuestions_new: Effect; //获取所有的试题
    getQuestions_condition: Effect; //按条件获取试题
    getQuestions_update: Effect; //更新试题
  };
  reducers: {
    save: Reducer<QuestionModelState>;
  };
  subscriptions: { setup: Subscription };
}

const UserModel: QuestionModelType = {
  namespace: 'question',
  state: {
    questionsType: [],
    subject: [],
    examType: [],
    getQuestions_newlist: [],
  },

  effects: {
    //获取所有的试题类型
    *getGetQuestionsType({ payload }, { call, put }) {
      let result = yield call(getGetQuestionsType);
      // console.log(result);
      if (result.code === 1) {
        yield put({
          type: 'save',
          payload: { questionsType: result.data },
        });
      }
    },
    //添加试题类型
    *getInsertQuestionsType({ payload }, { call, put }) {
      let result = yield call(getInsertQuestionsType, payload);
      // console.log(result);
      if (result.code === 1) {
        yield put({
          type: 'save',
        });
      }
    },
    //删除指定的试题类型
    *postDelQuestionsType({ payload }, { call, put }) {
      let result = yield call(postDelQuestionsType, payload);
      // console.log(result);
      if (result.code === 1) {
        yield put({
          type: 'save',
        });
      }
    },
    //获取所有的课程
    *getSubject({ payload }, { call, put }) {
      let result = yield call(getSubject);
      // console.log(result);
      if (result.code === 1) {
        yield put({
          type: 'save',
          payload: { subject: result.data },
        });
      }
    },
    //获取所有的考试类型
    *getExamType({ payload }, { call, put }) {
      let result = yield call(getExamType);
      // console.log(result);
      if (result.code === 1) {
        yield put({
          type: 'save',
          payload: { examType: result.data },
        });
      }
    },
    //添加试题接口
    *postQuestions({ payload }, { call, put }) {
      let result = yield call(postQuestions, payload);
      // console.log(result);
      if (result.code === 1) {
        yield put({
          type: 'save',
        });
      }
    },
    //获取所有的试题
    *getQuestions_new({ payload }, { call, put }) {
      let result = yield call(getQuestions_new);
      // console.log(result);
      if (result.code === 1) {
        yield put({
          type: 'save',
          payload: { getQuestions_newlist: result.data },
        });
      }
    },
    //按条件获取试题
    *getQuestions_condition({ payload }, { call, put }) {
      let result = yield call(getQuestions_condition, payload);
      // console.log(result);
      if (result.code === 1) {
        yield put({
          type: 'save',
          payload: { getQuestions_newlist: result.data },
        });
      }
    },
    //更新试题
    *getQuestions_update({ payload }, { call, put }) {
      let result = yield call(getQuestions_update, payload);
      // console.log(result);
      if (result.code === 1) {
        yield put({
          type: 'save',
        });
      }
    },
  },
  reducers: {
    save(state, action) {
      // console.log(state);
      return {
        ...state,
        ...action.payload,
      };
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({
            type: 'question',
          });
        }
      });
    },
  },
};

export default UserModel;
